<?php

namespace common\models\c2\entity;

use common\components\AttachmentBehavior;
use cza\base\models\statics\ImageSize;
use Yii;
use yii\behaviors\BlameableBehavior;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%material}}".
 *
 * @property integer $id
 * @property integer $type
 * @property string $label
 * @property resource $description
 * @property resource $is_auto
 * @property resource $cycle
 * @property integer $position
 * @property integer $status
 * @property integer $is_deleted
 * @property string $created_at
 * @property string $updated_at
 */
class Material extends \cza\base\models\ActiveRecord
{
    use \common\traits\AttachmentTrait;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%material}}';
    }

    public function fields()
    {
        return [
            'id',
            'label',
            'is_auto',
            'cycle',
            'album' => function ($model) {
                return $model->getInitialPreviewUrls('album', ImageSize::ORGINAL);
            },
        ];
    }

   public function behaviors()
   {
       return ArrayHelper::merge(parent::behaviors(), [
           // BlameableBehavior::className(),
           // \yii\behaviors\BlameableBehavior::className(), // record created_by and updated_by
           'attachmentsBehavior' => [
               'class' => AttachmentBehavior::className(),
               'attributesDefinition' => [
                   'album' => [
                       'class' => \common\models\c2\entity\EntityAttachmentImage::className(),
                       'validator' => 'image',
                       'rules' => [
                           'maxFiles' => 9,
                           'uploadRequired' => Yii::t('app.c2', 'Photo must upload.'),
                           // 'skipOnEmpty' => false,
                           // 'skipOnError' => false,
                           'extensions' => Yii::$app->params['config']['upload']['imageWhiteExts'],
                           // 'extensions' => ['jpg'],
                           'maxSize' => Yii::$app->params['config']['upload']['maxFileSize'],
                           // 'maxSize' => '2048',
                       ]
                   ]
               ]
           ],
       ]); // TODO: Change the autogenerated stub
   }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['position', 'type', 'is_auto', 'cycle'], 'integer'],
            [['created_at', 'updated_at', 'album'], 'safe'],
            [['label', 'description'], 'string', 'max' => 255],
            [['status', 'is_deleted'], 'integer', 'max' => 4],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'type' => Yii::t('app.c2', 'Type'),
            'label' => Yii::t('app.c2', 'Label'),
            'description' => Yii::t('app.c2', 'Description'),
            'position' => Yii::t('app.c2', 'Position'),
            'is_auto' => Yii::t('app.c2', 'Is Auto'),
            'cycle' => Yii::t('app.c2', 'Cycle'),
            'status' => Yii::t('app.c2', 'Status'),
            'is_deleted' => Yii::t('app.c2', 'Is Deleted'),
            'created_at' => Yii::t('app.c2', 'Created At'),
            'updated_at' => Yii::t('app.c2', 'Updated At'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\MaterialQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\MaterialQuery(get_called_class());
    }
    
    /**
    * setup default values
    **/
    public function loadDefaultValues($skipIfSet = true) {
        parent::loadDefaultValues($skipIfSet);
    }

}
